package com.library.controller;

import com.library.pojo.dto.book.BookDTO;
import com.library.pojo.dto.book.BookPageQueryDTO;
import com.library.result.PageResult;
import com.library.result.Result;
import com.library.service.BookService;
import com.library.service.impl.BookServiceImpl;

/**
 * 管理员对图书进行增删改查
 * 只写对外的方法
 */
public class AdminBookController {

    private final BookService bookService = new BookServiceImpl();

    /**
     * 分页查询图书
     * @param bookPageQueryDTO 分页参数（必填：pageNum/pageSize；可选：name/author/publisher/）
     * @return PageResult<LibraryBooks>：分页图书列表
     */
    public Result<PageResult> pageQuery(BookPageQueryDTO bookPageQueryDTO) {
        PageResult pageResult = bookService.pageQuery(bookPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 添加图书
     * @param bookDTO 添加参数（可选：name/author/publisher/price/stock等）
     * @return Result<String>：成功/失败提示
     */
    public Result<String> insert(BookDTO bookDTO) {
        int flag = bookService.insert(bookDTO);
        if(flag == 1) {
            return Result.success();
        }

        return Result.error("图书信息有误，添加图书失败");
    }

    /**
     * 根据图书编号修改图书信息
     *
     * @param bookDTO 修改参数（必填：bookId；可选：name/author/publisher/price/stock/loan等）
     * @return Result<String>：成功/失败提示
     */
    public Result<String> update(BookDTO bookDTO) {
        int flag = bookService.update(bookDTO);
        if(flag == 1) {
            return Result.success();
        }
        return Result.error("修改图书信息失败");
    }

    /**
     * 根据图书编号删除图书
     * @param bookId 图书编号
     * @return 删除成功返回1，失败返回0
     */
    public Result<String> deleteById(Integer bookId) {
        BookServiceImpl bookService = new BookServiceImpl();
        int flag = bookService.deleteById(bookId);
        if(flag == 1) {
            return Result.success();
        }
        return Result.error("要删除的图书不存在");
    }
}
